import axios from 'axios'
import { Loading } from "element-ui";

import router from '../router'

const PRODUCT_URL = 'https://www.ccclkj.cn:2022/'
// const MOCK_URL = 'https://www.ccclkj.cn:2022/'
const MOCK_URL = 'http://192.168.0.101:2022/'
export const baseurl = process.env.NODE_ENV === 'production' ? PRODUCT_URL : MOCK_URL;

export function requestNews(config) {
    const instance = axios.create({
        baseURL: baseurl,
    });
    const loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)"
    });
    instance.interceptors.request.use(config => {
        console.log(config);
        config.headers.Authorization = window.sessionStorage.getItem('Authorization')
        return config
    }, err => {
        console.log(err)
    })

    instance.interceptors.response.use(res => {
        console.log("这里000");
        console.log(res);
        loading.close();
        if (res.data != null) {
            if (res.data.msg == '没有token') {
                window.sessionStorage.setItem('timeout', 'true');
                router.replace("/login");
            }
        }
        return res.data
    }, err => {
        console.log(err)
    })

    return instance(config)
}